PHP 条件语句


2022年8月17日, Learn eTutorial
2081

在本 PHP 教程中,您将学习有关 PHP 中条件语句的所有知识。我们将详细讨论不同类型的条件语句,即 if、if … else、if… else if … else、嵌套 if、switch、三元运算符

什么是 PHP 中的 if 语句?

如果给定条件为真,if 语句将执行其代码块。

语法


if (condition) {
  code to be executed if condition is true;
}
 
PHP - If

示例:PHP 中的 if 语句


$a = 20;
$b = 18;
if($a > $b){
    echo "$a is the greatest…";
}

 

输出


20 is the greatest…

在上面的示例中,我们比较了两个变量并打印出其中较大的一个。在这个程序中,我们使用 if 语句,检查条件“a > b”,如果条件为真,我们将打印变量 a 的值是最大的并退出程序,如果条件为假,则我们将退出程序。

什么是 PHP 中的 if … else 语句?

在 if … else 语句中,我们首先检查条件,如果条件为真,我们执行 if 代码块,如果条件为假,则我们执行 else 代码块。

语法


if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}
 
PHP - If

示例:PHP 中的 if 语句


$a = 10;
$b = 17;
if($a > $b){
    echo "$a is the greatest…";
} else {
    echo "$b is the greatest…";
}

 

输出


17 is the greatest…

PHP 中的 if … else if … else 语句

如果我们需要执行多个条件检查,并且对于每个条件,我们都必须执行不同的代码块,这时我们使用 if … else if … else 语句。它首先检查 if 中的条件,如果为真,则执行 if 代码块;如果为假,则检查 else if 中的条件,如果为真,则执行其代码块。我们可以使用多个 else if 块,如果每个条件都为假,则默认执行 else 块。

语法


if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

 
PHP - If

示例:PHP 中的 if … else if … else 语句


$a = 10;
$b = 17;
$c = 8;
if($a > $b && $a > $c){
    echo "$a is greatest...";
}
else if($b > $c){
    echo "$b is greatest...";
}
else{
    echo "$c is greatest...";
}
 

输出


17 is the greatest…

在上面的示例中,我们比较了三个变量并打印出其中最大的一个。在这个程序中,我们使用 if … else if … else 语句,检查条件“a > b”和“a > c”,如果两者都为真,我们将打印变量“a”的值是最大的并退出程序;如果为假,则我们将检查条件“b > c”,如果为真,则我们打印变量“b”的值是最大的;如果两个条件都为假,我们打印变量“c”的值是最大的。

什么是 PHP 中的嵌套 if 语句?

在嵌套 if 语句中,if 代码块将包含另一个 if 语句。这个内部的 if 语句只有在外部条件为真时才会执行。

语法


if (condition) {    
code to be executed if condition is true   
if (condition) {    
code to be executed if condition is true    
}    
}  
 
PHP - Nested If

示例:PHP 中的嵌套 if 语句


$a = 19;
$b = 17;
$c = 15;
if($a > $b){
    if($a > $c){
        echo "$a is greatest...";
    }
}
else if($b > $c){
    echo "$b is greatest...";
}
else{
    echo "$c is greatest...";
}

 

输出


19 is greatest...

在上面的示例中,我们比较了三个变量并打印出其中最大的一个。在这个程序中,我们使用嵌套 if 语句,检查条件“a > b”,如果为真,我们接着检查条件“a > c”,如果为真,我们打印变量“a”的值是最大的并退出程序;如果为假,则我们将退出外部 if 代码块,然后检查条件“b > c”,如果为真,则我们打印变量“b”的值是最大的;如果两个条件都为假,我们打印变量“c”的值是最大的。

什么是 PHP 中的 switch 语句?

在 switch 语句中,我们传递一个表达式,然后检查不同的 case,对于每个匹配项执行不同的语句。最后将执行 default 块。

语法


switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
    ...
  default:
    code to be executed if n is different from all labels;
}

 
PHP - If

示例:PHP 中的 if 语句


$ch = 6;  
    switch ($ch)  
    {     
        case 1:   
            echo "Monday";  
            break;  
        case 2:   
            echo "Tuesday";  
            break;  
        case 3:   
            echo "Wednesday";  
            break;  
        case 4:   
            echo "Thursday";  
            break; 
        case 5:   
            echo "Friday";  
            break;  
        case 6:   
            echo "Saturday";  
            break;  
        case 7:   
            echo "Sunday";  
            break; 
        default:   
            echo "Wrong Choice";  
            break;  
    }  
 

输出


Saturday

在这个程序中,我们将打印星期几。为此,我们首先将表示日期的数字赋给变量“ch”,然后在 switch 语句中传递变量“ch”的值,接着在 switch 语句块中为每个选项编写 case。最后,我们使用一个 default 语句来执行所有 case 都不匹配时的操作。